<template>
  <el-container class='home-con'>
    <!--      头部区域-->
    <el-header>
      <div class='el-div'>
        <span>校园墙WEB后台管理系统</span>
      </div>
      <el-button type='info' @click='logOut'>退出</el-button>
    </el-header>
    <!--      页面主体区-->
    <el-container>
      <!--        左侧菜单栏-->
      <el-aside :width="isCollapse?'64px':'200px'">
        <div class='toggle' @click='toggleBtn'>
          <i class='el-icon-s-fold'></i>
        </div>
        <!--        侧边栏菜单区域
            active-text-color表示当鼠标点击菜单栏时 菜单栏的字体的颜色
             unique-opened表示只能打开一个子菜单栏 collapse折叠菜单-->
        <el-menu
          router
          background-color='#545c64'
          text-color='#fff'
          active-text-color='#409EFF'
          unique-opened
          :collapse-transition='false'
          :collapse='isCollapse'
          :default-active='activePath'>
          <!--          这是一级菜单-->
          <el-submenu v-for='item in menusList' :index="item.id+''" v-bind:key="item.id+''">
            <!--            这是一级菜单的模板区-->
            <template slot='title'>
              <!--              这是图标-->
              <i v-bind:class='item.CSS'></i>
              <!--              文本-->
              <span>{{ item.authName }}</span>
            </template>
            <!--           二级菜单 注意这里遍历的时候 是每个{} 每个{}代表一个对象-->
            <el-menu-item v-for='item1 in item.children'
                          :index='item1.path'
                          :key='item1.id'
                          @click='saveBtn(item1.path)'>
              <template slot='title'>
                <!--                图标-->
                <i class='el-icon-menu'></i>
                <!--                文本-->
                <span>{{ item1.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside><!--        右侧内容-->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>

</template>

<script>
import axios from 'axios'

export default {
  name: "home",
  data() {
    return {
      menusList: [],
      //是否折叠 默认为false表示未折叠
      isCollapse: false,
      //
      activePath: ''
    }
  },
  methods: {
    //保存链接激活的状态
    saveBtn(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
    //是否将菜单进行折叠效果
    toggleBtn() {
      this.isCollapse = !this.isCollapse
    },
    logOut() {
      //销毁本地的token 并且重新定向路由到主界面
      sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList() {
      let _this = this
      axios.get('http://localhost:8080/Manger.json').then(function(resp) {
        if (resp.data.meta.status !== 200) {
          _this.$message.error('加载失败！')
        }
        _this.menusList = resp.data.date
      })
    }
  },
  created() {
    //判断有没有token
    let item = sessionStorage.getItem("token");
    if (item == null) {
      this.$router.push("/login")
    } else {
      //  有的话 是否过期
      const expireTime = sessionStorage.getItem("tokenExpireTime");
      const time = new Date().getTime();
      if (expireTime - time < 0) {
        this.$router.push("/login")
      }
    }
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  }
}
</script>


<style scoped>
.el-menu {
  border-right: none;
}

.el-header {
  background-color: #373d41;
}

.el-aside {
  background-color: #545c64;
}

.el-main {
  background-color: #eaedf1;
}

/*这是为了让标签撑满整个页面*/
.home-con {
  height: 100vh;
}

.el-header {
  display: flex;
  /*左右分开对齐*/
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: white;
  font-size: 20px;
}

.el-div {
  display: flex;
  /*纵向居中对齐 文本*/
  align-items: center;
}

.el-div span {
  margin-left: 10px;
}

.toggle {
  background-color: #4A5064;
  font-size: 10px;
  line-height: 24px;
  color: white;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
